package ui.activity.wallet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import net.ck.hzq.R;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Utils.StringUtil;
import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import coom.Constants;
import event.NetErrorEvent;
import ui.activity.wallet.biz.RewardsDetailsBiz;
import ui.activity.wallet.component.DaggerRewardsDetailsComponent;
import ui.activity.wallet.contract.RewardsDetailsContract;
import ui.activity.wallet.module.RewardsDetailsModule;
import ui.activity.wallet.presenter.RewardsDetailsPresenter;
import ui.adapter.RewardsDetailsAdapter;
import ui.adapter.RewardsDetailsChallengeAdapter;
import ui.adapter.RewardsDetailsGrowthTopAdapter;
import ui.model.ReardsGrowthBean;
import ui.model.RewardsChallengeBean;
import ui.model.RewardsDetailsBean;
import util.AlertUtils;
import util.SysUtils;


/**
 * 新钱包奖励列表
 * Created by chenjian on 2017/8/4.
 */

public class RewardsDetailsAct extends BaseAct implements RewardsDetailsContract.View {


    @Inject
    RewardsDetailsPresenter presenter;
    @Inject
    RewardsDetailsBiz biz;

    @BindView(R.id.profit_day)
    TextView dayText;
    @BindView(R.id.profit_check)
    TextView check;
    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    View headerView;
    TextView time;
    TextView totalMoney;
    long selectTime;
    RewardsDetailsAdapter adp;//普通适配器
    RewardsDetailsGrowthTopAdapter adapter;//成长奖适配器
    RewardsDetailsChallengeAdapter challengeAdapter;//挑战奖适配器
    RewardsDetailsBean rewardsBean;
    private int year;
    private int month;

    String[] infoStr = {"提现记录", "购买奖", "推广奖", "培养奖", "会员奖", "成长奖", "挑战奖", "导师奖"};

    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_rewards_details);
    }

    @Override
    protected void setup() {
        DaggerRewardsDetailsComponent.builder().rewardsDetailsModule(new RewardsDetailsModule(this)).build().inject(this);
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        rewardsBean = (RewardsDetailsBean) getIntent().getSerializableExtra(Constants.KEY);
        if (rewardsBean.getType() > 0 && rewardsBean.getType() < infoStr.length) {
            setToolBar(infoStr[rewardsBean.getType()], "");
        }
        dayText.setText(SysUtils.sf3.format(new Date(System.currentTimeMillis())));
        headerView = View.inflate(this, R.layout.item_rewards_details_top, null);
        time = headerView.findViewById(R.id.item_rewards_details_top_time);
        totalMoney = headerView.findViewById(R.id.item_rewards_details_top_money);


        selectTime = System.currentTimeMillis();
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeInMillis(selectTime);
//        year = calendar.get(Calendar.YEAR);
//        month = calendar.get(Calendar.MONTH) + 1;
        year = rewardsBean.getYear();
        month = rewardsBean.getMonth();
        RxView.touches(dayText)
                .compose(this.<MotionEvent>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(MotionEvent -> {
                    TimePickerDialog timePickerDialog = AlertUtils.timePicker3(RewardsDetailsAct.this, 365 * 5, 0, selectTime, new OnDateSetListener() {
                        @Override
                        public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                            selectTime = millseconds;
                            dayText.setText(SysUtils.sf3.format(new Date(millseconds)));
                        }
                    });
                    timePickerDialog.show(RewardsDetailsAct.this.getSupportFragmentManager(), "all");
                });

        RxView.clicks(check)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    presenter.getListInfo(true, rewardsBean.getType(), selectTime, year, month);
                });
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                presenter.getListInfo(false, rewardsBean.getType(), selectTime, year, month);
            }
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getListInfo(true, rewardsBean.getType(), selectTime, year, month);
            }
        });
        ptr.autoRefresh(true);
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }

    @Override
    public void upDateUI(List<RewardsDetailsBean> bean) {
        if (adp == null) {
            adp = new RewardsDetailsAdapter(RewardsDetailsAct.this, bean, rewardsBean.getType());
            adp.addHeaderView(headerView);
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }
        time.setText(rewardsBean.getYear() + "." + rewardsBean.getMonth());
        totalMoney.setText("奖励金额：" + StringUtil.getFormatPrice(rewardsBean.getAmount()) + "元");
    }

    @Override
    public void upDateUi(List<ReardsGrowthBean.RecordsBean> bean) {
        if (adapter == null) {
            adapter = new RewardsDetailsGrowthTopAdapter(RewardsDetailsAct.this, bean);
            rv.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void upDateUIinfo(List<RewardsChallengeBean.RecordsBean> bean) {
        if (challengeAdapter == null) {
            challengeAdapter = new RewardsDetailsChallengeAdapter(RewardsDetailsAct.this, bean);
            challengeAdapter.addHeaderView(headerView);
            rv.setAdapter(challengeAdapter);
        } else {
            challengeAdapter.notifyDataSetChanged();
        }
        time.setText(rewardsBean.getYear() + "." + rewardsBean.getMonth());
        totalMoney.setText("奖励金额：" + StringUtil.getFormatPrice(rewardsBean.getAmount()) + "元");

    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }

}
